<?php
namespace app\index\controller;

use app\common\model\ZhEpisode as ZhEpisodeModel;
use app\common\model\ZhVideo as ZhVideoModel;

/**
 * 寺院剧集控制器
 * Class Zhepisode
 * @package app\index\controller
 */
class Zhepisode extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new ZhEpisodeModel;
        $this->getPageAndSize($param);
        $map = [];
        try {
            if(!empty($param['title'])){
                $map['title'] = $param['title'];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['states'] = ['neq',3];
            $map['vid'] = $param['vid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,'id,title,intro,number,img,url,create_time,size');
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看视频列表');
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [add  添加]
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new ZhEpisodeModel;
        $user = $this->user;
        try{
            $param['comid'] = $user['comid'];
            $model->addData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'添加成功');
    }
    /**
     * [edit  banner编辑]
     * @return json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new ZhEpisodeModel;
        $zmodel = new ZhVideoModel;
        $user = $this->user;
        try{
            $banner = $model->findData($param['id'],'id','vid');
            $video = $zmodel->findData($banner['vid'],'id','comid');
            if($video['comid'] != $user['comid']){
                return admin_json(-1,'编辑失败');
            }
            $model->editData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'编辑成功');
    }
    /**
     * [del  banner删除]
     * @return json
     * @author [C。]
     */
    public function del()
    {
        $param = input('param.');
        $model = new ZhEpisodeModel;
        $zmodel = new ZhVideoModel;
        $user = $this->user;
        try{
            $banner = $model->findData($param['id'],'id','vid');
            $video = $zmodel->findData($banner['vid'],'id','comid');
            if($video['comid'] != $user['comid']){
                return admin_json(-1,'删除失败');
            }
            $model->editData(['id'=>$param['id'],'states'=>3]);
        }catch (\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'删除成功');
    }
    /**
     * [sort  排序]
     * @return json
     * @author [C。]
     */
    public function sort()
    {
        $param = input('param.');
        $model = new ZhEpisodeModel;
        try{
            foreach($param['sort'] as &$v){
                $model->editData($v);
            }
        }catch (\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'排序成功');
    }
}